package com.szx.ssm.servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szx.ssm.bean.tbDept;
import com.szx.ssm.bean.tbEmp;
import com.szx.ssm.bean.tbEmpExample;
import com.szx.ssm.dao.tbDeptMapper;
import com.szx.ssm.dao.tbEmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author songzhengxiang
 * @create 2022-06-29 23:28
 */
@Service
public class tbEmpServlet {
    @Autowired
    tbEmpMapper tbEmpMapper;
    @Autowired
    tbDeptMapper tbDeptMapper;

    /**
     * 根据页码查询
     * @author Songzx
     * @date 2022/7/2
     */
    public PageInfo getEmpList(Integer pageNumber){
        PageHelper.startPage(pageNumber,10);
        // 获取所有员工
        List<tbEmp> empList = tbEmpMapper.selectByExample(null);
        // 根据员工获取部门信息
        for (tbEmp tbEmp : empList) {
            tbDept dept = tbDeptMapper.selectByPrimaryKey(tbEmp.getdId());
            tbEmp.setTbDept(dept);
        }
        // 获取分页信息
        PageInfo<tbEmp> pageInfo = new PageInfo<>(empList,5);
        // 返回分页信息
        return pageInfo;
    }

    /**
     * 根据姓名模糊查询
     * @author Songzx
     * @date 2022/7/2
     */
    public PageInfo getEmpListByName(Integer pageNum,String name){
        PageHelper.startPage(pageNum,10);
        tbEmpExample example = new tbEmpExample();
        example.createCriteria().andEmpNameLike("%"+name+"%");
        List<tbEmp> tbEmps = tbEmpMapper.selectByExample(example);
        for (tbEmp tbEmp : tbEmps) {
            tbDept dept = tbDeptMapper.selectByPrimaryKey(tbEmp.getdId());
            tbEmp.setTbDept(dept);
        }
        PageInfo<tbEmp> pageInfo = new PageInfo<>(tbEmps,5);
        return pageInfo;
    }


    /**
     * 根据id删除
     * @author Songzx
     * @date 2022/7/3
     */
    public int deleteEmpById(Integer id){
        int i = tbEmpMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 新增员工
     * @author Songzx
     * @date 2022/7/3
     */
    public int addEmpInfo(tbEmp tbEmp){
        int insert = tbEmpMapper.insertSelective(tbEmp);
        return insert;
    }

    /**
     * 编辑员工
     * @author Songzx
     * @date 2022/7/3
     */
    public int updateEmp(tbEmp tbEmp){
        int i = tbEmpMapper.updateByPrimaryKeySelective(tbEmp);
        return i;
    }

    /**
     * 批量删除
     * @author Songzx
     * @date 2022/7/3
     */
    public void deletesEmp(List<Integer> ids){
        for (Integer id : ids) {
            tbEmpMapper.deleteByPrimaryKey(id);
        }
    }

}
